import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'
import ProfileView from '../views/ProfileView.vue'
import AdminLayout from '../layouts/AdminLayout.vue'
import TeacherLayout from '../layouts/TeacherLayout.vue'
import AssistantLayout from '../layouts/AssistantLayout.vue'
import ParentLayout from '../layouts/ParentLayout.vue'
import Overview from '../views/admin/Overview.vue'
import TeacherVerification from '../views/admin/TeacherVerification.vue'
import AdminSettings from '../views/admin/AdminSettings.vue'
import RoleManagement from '../views/admin/RoleManagement.vue'
import PermissionManagement from '../views/admin/PermissionManagement.vue'
import UserRoleManagement from '../views/admin/UserRoleManagement.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView
  },
  {
    path: '/profile',
    name: 'profile',
    component: ProfileView,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin',
    component: AdminLayout,
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: '',
        name: 'admin-dashboard',
        component: Overview,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'teachers',
        name: 'teacher-verification',
        component: TeacherVerification,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'settings',
        name: 'admin-settings',
        component: AdminSettings,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'roles',
        name: 'role-management',
        component: RoleManagement,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'permissions',
        name: 'permission-management',
        component: PermissionManagement,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'user-roles',
        name: 'user-role-management',
        component: UserRoleManagement,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'bookings',
        name: 'booking-management',
        component: () => import('../components/admin/BookingManagement.vue'),
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'courses',
        name: 'course-management',
        component: () => import('../components/admin/CourseManagement.vue'),
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'schedules',
        name: 'schedule-management',
        component: () => import('../components/admin/ScheduleManagement.vue'),
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'users',
        name: 'user-management',
        component: () => import('../components/admin/UserManagement.vue'),
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'orders',
        name: 'order-management',
        component: () => import('../components/admin/OrderManagement.vue'),
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'youbi',
        name: 'youbi-management',
        component: () => import('../components/admin/YoubiManagement.vue'),
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'messages',
        name: 'message-management',
        component: () => import('../components/admin/MessageManagement.vue'),
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'menus',
        name: 'menu-management',
        component: () => import('../components/admin/MenuManagement.vue'),
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: ':pathMatch(.*)*',  // 通配符路由，处理所有未明确匹配的/admin/*路径
        name: 'admin-dynamic',
        component: () => import('../views/admin/DynamicView.vue'),  // 使用动态视图组件来渲染具体的管理功能
        meta: { requiresAuth: true, requiresAdmin: true }
      }
    ]
  },
  {
    path: '/teacher',
    component: TeacherLayout,
    meta: { requiresAuth: true, requiresTeacher: true },
    children: [
      {
        path: '',
        name: 'teacher-profile',
        component: () => import('../components/teacher/TeacherManagement.vue'),
        meta: { requiresAuth: true, requiresTeacher: true }
      },
      {
        path: 'profile',
        name: 'teacher-profile',
        component: () => import('../components/teacher/TeacherManagement.vue'),
        meta: { requiresAuth: true, requiresTeacher: true }
      },
      {
        path: 'courses',
        name: 'teacher-courses',
        component: () => import('../components/teacher/TeacherManagement.vue'),
        meta: { requiresAuth: true, requiresTeacher: true }
      },
      {
        path: 'schedule',
        name: 'teacher-schedule',
        component: () => import('../components/teacher/TeacherManagement.vue'),
        meta: { requiresAuth: true, requiresTeacher: true }
      },
      {
        path: 'bookings',
        name: 'teacher-bookings',
        component: () => import('../components/teacher/TeacherManagement.vue'),
        meta: { requiresAuth: true, requiresTeacher: true }
      },
      {
        path: 'orders',
        name: 'teacher-orders',
        component: () => import('../components/teacher/TeacherManagement.vue'),
        meta: { requiresAuth: true, requiresTeacher: true }
      },
      {
        path: 'teaching',
        name: 'teacher-teaching',
        component: () => import('../components/teacher/TeacherManagement.vue'),
        meta: { requiresAuth: true, requiresTeacher: true }
      },
      {
        path: 'youbi',
        name: 'teacher-youbi',
        component: () => import('../components/teacher/TeacherManagement.vue'),
        meta: { requiresAuth: true, requiresTeacher: true }
      },
      {
        path: 'messages',
        name: 'teacher-message',
        component: () => import('../components/teacher/TeacherManagement.vue'),
        meta: { requiresAuth: true, requiresTeacher: true }
      },
      {
        path: ':pathMatch(.*)*',  // 通配符路由，处理所有/teacher/*路径
        name: 'teacher-dynamic',
        component: TeacherLayout,
        meta: { requiresAuth: true, requiresTeacher: true }
      }
    ]
  },
  {
    path: '/assistant',
    component: AssistantLayout,
    meta: { requiresAuth: true, requiresAssistant: true },
    children: [
      {
        path: '',
        name: 'assistant-profile',
        component: () => import('../components/assistant/AssistantManagement.vue'),
        meta: { requiresAuth: true, requiresAssistant: true }
      },
      {
        path: 'profile',
        name: 'assistant-profile',
        component: () => import('../components/assistant/AssistantManagement.vue'),
        meta: { requiresAuth: true, requiresAssistant: true }
      },
      {
        path: 'teachers',
        name: 'assistant-teachers',
        component: () => import('../components/assistant/TeacherManagement.vue'),
        meta: { requiresAuth: true, requiresAssistant: true }
      },
      {
        path: 'messages',
        name: 'assistant-messages',
        component: () => import('../components/admin/MessageManagement.vue'),
        meta: { requiresAuth: true, requiresAssistant: true }
      },
      {
        path: 'youbi',
        name: 'assistant-youbi',
        component: () => import('../components/admin/YoubiManagement.vue'),
        meta: { requiresAuth: true, requiresAssistant: true }
      },
      {
        path: ':pathMatch(.*)*',  // 通配符路由，处理所有/assistant/*路径
        name: 'assistant-dynamic',
        component: AssistantLayout,
        meta: { requiresAuth: true, requiresAssistant: true }
      }
    ]
  },
  {
    path: '/parent',
    component: ParentLayout,
    meta: { requiresAuth: true, requiresParent: true },
    children: [
      {
        path: '',
        name: 'parent-profile',
        component: () => import('../components/parent/ParentManagement.vue'),
        meta: { requiresAuth: true, requiresParent: true }
      },
      {
        path: 'profile',
        name: 'parent-profile',
        component: () => import('../components/parent/ParentManagement.vue'),
        meta: { requiresAuth: true, requiresParent: true }
      },
      {
        path: 'children',
        name: 'parent-children',
        component: () => import('../components/parent/ChildrenManagement.vue'),
        meta: { requiresAuth: true, requiresParent: true }
      },
      {
        path: 'bookings',
        name: 'parent-bookings',
        component: () => import('../components/admin/BookingManagement.vue'),
        meta: { requiresAuth: true, requiresParent: true }
      },
      {
        path: 'orders',
        name: 'parent-orders',
        component: () => import('../components/admin/OrderManagement.vue'),
        meta: { requiresAuth: true, requiresParent: true }
      },
      {
        path: 'youbi',
        name: 'parent-youbi',
        component: () => import('../components/admin/YoubiManagement.vue'),
        meta: { requiresAuth: true, requiresParent: true }
      },
      {
        path: 'messages',
        name: 'parent-messages',
        component: () => import('../components/admin/MessageManagement.vue'),
        meta: { requiresAuth: true, requiresParent: true }
      },
      {
        path: ':pathMatch(.*)*',  // 通配符路由，处理所有/parent/*路径
        name: 'parent-dynamic',
        component: ParentLayout,
        meta: { requiresAuth: true, requiresParent: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// Navigation guard for protected routes
import { useAuthStore } from '@/stores/auth';

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const authStore = useAuthStore();
  
  // 确保authStore已初始化
  if (token && !authStore.token) {
    authStore.$patch({
      token: token,
      user: JSON.parse(localStorage.getItem('authStore'))?.user || null
    });
  }

  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else if (to.meta.requiresAdmin && (!authStore.user || authStore.user.roleId !== 'ADMIN')) {
    // If user is not admin, redirect to home
    next('/')
  } else if (to.meta.requiresTeacher && (!authStore.user || authStore.user.roleId !== 'TEACHER')) {
    // If user is not teacher, redirect to home
    next('/')
  } else if (to.meta.requiresParent && (!authStore.user || authStore.user.roleId !== 'PARENT')) {
    // If user is not parent, redirect to home
    next('/')
  } else if (to.meta.requiresAssistant && (!authStore.user || authStore.user.roleId !== 'ASSISTANT')) {
    // If user is not assistant, redirect to home
    next('/')
  } else {
    next()
  }
})

export default router